package com.cy.web.controller;

import com.cy.common.util.PageUtil;
import com.cy.dao.pojo.SysNotice;
import com.cy.service.SysNoticeService;
import com.cy.web.pojo.JsonResult;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 此对象为公告模块控制层处理器对象
 *  1. 请求url设计：
 *  1）条件查询 (请求方式get，请求url:/notice/)
 */

//@ResponseBody
//@Controller
@CrossOrigin
@RestController
@RequestMapping("/notice/")
public class SysNoticeController {
    @Autowired
    private SysNoticeService sysNoticeService;

    //请求url: http://localhost/notice/
    @GetMapping
    public JsonResult doSelectNotices(SysNotice sysNotice){
        //启动分页查询拦截
        return new JsonResult(PageUtil.startPage().
                doSelectPageInfo(() -> sysNoticeService.selectNotice(sysNotice)));
//        return new JsonResult(sysNoticeService.selectNotice(sysNotice));
        //这个结果会返回给调用方(DispatcherServlet)
        //方法调用方拿到方法执行结果以后会调用相关的api将结果转换为json字符串
        //最后通过响应对象，将字符串响应到客户端
    }

    @PostMapping
    public JsonResult doInsertNotices(@RequestBody SysNotice sysNotice){
        sysNoticeService.insertNotice(sysNotice);
        return new JsonResult("save ok");
    }

    @GetMapping("{id}")
    public JsonResult doSelectById(@PathVariable Long id){
            return new JsonResult(sysNoticeService.selectById(id));
//        }catch (Exception e){
//            return new JsonResult(e);
//        }

    }

    @PutMapping
    public JsonResult doUpdateNotices(@RequestBody SysNotice sysNotice){

        sysNoticeService.updateNotice(sysNotice);
        return new JsonResult("update ok");
    }

    @DeleteMapping("{id}")
    public JsonResult doDeleteById(@PathVariable Long... id){
        sysNoticeService.deleteById(id);
        return new JsonResult("delete ok");

    }
}
